package com.example.othree.controller;

import com.example.othree.common.DataUtil;
import com.example.othree.common.R;
import com.example.othree.entity.Lidar;
import com.example.othree.mapper.LidarMapper;
import com.example.othree.service.LidarService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@CrossOrigin
@RequestMapping("/api")
public class LidarOzoneController {

    @Autowired
    LidarService lidarService;

    @ApiOperation("获取臭氧激光雷达的观测数据")
    @GetMapping("/lidar-data-fixed")
    // TODO 根据给定的站点Station、查询要素Yaosu、
    //  查询时间段Stime、Endtime和查询方式Type（原始10分钟、小时平均、日平均、月平均），
    //  得到json数组。
    public R lidarDataFixed(@RequestParam("station") String station,
                            @RequestParam("yaosu") String yaosu,
                            @RequestParam("startTime")String startTime,
                            @RequestParam("endTime")String endTime,
                            @RequestParam("type")String type) {

        Map<String, Object> dataReturn = new HashMap<>();
        try {
            if ("衡水".equals(station)) station = "饶阳";
            if("臭氧浓度".equals(yaosu)){
                yaosu = "CO3";
            }else if ("消光系数".equals(yaosu)){
                yaosu = "extino3";
            }else if("A通道距离平方".equals(yaosu)) {
                yaosu = "chaprr";
            }else if("A通道SNR".equals(yaosu)){
                yaosu = "chasnr";
            }

            if (type.equals("dayAVG")){
                dataReturn = lidarService.toFindByStationsDay(station, yaosu, startTime, endTime);
            } else if (type.equals("monthAVG")) {
                dataReturn = lidarService.toFindByStationsMouth(station, yaosu, startTime, endTime);
            } else if (type.equals("hourAVG")) {
                dataReturn = lidarService.toFindByStationsHour(station, yaosu, startTime, endTime);
            } else if (type.equals("min10")) {
                dataReturn = lidarService.toFindByStationsMin(station, yaosu, startTime, endTime);
            }

            dataReturn.put("station", station);
            dataReturn.put("yaosu", yaosu);
            dataReturn.put("Stime", startTime);
            dataReturn.put("Endtime", endTime);
            dataReturn.put("Type", type);


        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return R.error().message("查询要素数据为空");
        }catch (DataAccessException e){
            e.printStackTrace();
            return R.error().message("数据库访问出错");
        } catch (Exception e){
            e.printStackTrace();
            return R.error().message("出现了未知错误");
        }

        return R.ok().data("dataReturn",dataReturn);
    }

}
